/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.base.web.warehouse;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.NumberUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.base.entity.customer.Customer;
import com.jeesite.modules.base.entity.supplier.Supplier;
import com.jeesite.modules.base.entity.warehouse.Twarehouse;
import com.jeesite.modules.base.entity.warehouse.TwarehouseChargeItems;
import com.jeesite.modules.base.service.customer.CustomerService;
import com.jeesite.modules.base.service.supplier.SupplierService;
import com.jeesite.modules.base.service.warehouse.TwarehouseService;
import com.jeesite.modules.custom.enumInfo.warehourseEnum;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 仓库档案Controller
 *
 * @author shengbin
 * @version 2021-03-01
 */
@Controller
@RequestMapping(value = "${adminPath}/base/warehouse/twarehouse")
public class TwarehouseController extends BaseController {

    @Autowired
    private TwarehouseService twarehouseService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private UserService userService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public Twarehouse get(String warehouseId, boolean isNewRecord) {
        return twarehouseService.get(warehouseId, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("base:warehouse:twarehouse:view")
    @RequestMapping(value = {"list", ""})
    public String list(Twarehouse twarehouse, Model model) {
        List < Customer > listCustomer = customerService.findList(new Customer());

        Supplier supplier = new Supplier();
        supplier.setSupplierType(warehourseEnum.HAIWAIZIFANG.getCode());//海外资方
        List < Supplier > listSupplierZF = supplierService.findList(supplier);

        Supplier supplier1 = new Supplier();
        supplier1.setSupplierType(warehourseEnum.CABGKUFUWUSHANG.getCode());//仓库服务商
        List < Supplier > listSupplierFWS = supplierService.findList(supplier1);

        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("listCustomer", listCustomer);
        model.addAttribute("listSupplierFWS", listSupplierFWS);
        model.addAttribute("listSupplierZF", listSupplierZF);
        model.addAttribute("twarehouse", twarehouse);
        return "modules/base/warehouse/twarehouseList";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("base:warehouse:twarehouse:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page < Twarehouse > listData(Twarehouse twarehouse, HttpServletRequest request, HttpServletResponse response) {
        twarehouse.setPage(new Page <>(request, response));
        Page < Twarehouse > page = twarehouseService.findPage(twarehouse);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("base:warehouse:twarehouse:view")
    @RequestMapping(value = "form")
    public String form(Twarehouse twarehouse, Model model) {
        List < Customer > listCustomer = customerService.findList(new Customer());

        Supplier supplier = new Supplier();
        supplier.setSupplierType(warehourseEnum.HAIWAIZIFANG.getCode());//海外资方
        List < Supplier > listSupplierZF = supplierService.findList(supplier);

        Supplier supplier1 = new Supplier();
        supplier1.setSupplierType(warehourseEnum.CABGKUFUWUSHANG.getCode());//仓库服务商
        List < Supplier > listSupplierFWS = supplierService.findList(supplier1);

        model.addAttribute("listCustomer", listCustomer);
        model.addAttribute("listSupplierFWS", listSupplierFWS);
        model.addAttribute("listSupplierZF", listSupplierZF);
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("twarehouse", twarehouse);
        return "modules/base/warehouse/twarehouseForm";
    }

    /**
     * 查看表单详情
     */
    @RequiresPermissions("base:warehouse:twarehouse:view")
    @RequestMapping(value = "detail")
    public String detail(Twarehouse twarehouse, Model model) {
        List < Customer > listCustomer = customerService.findList(new Customer());

        Supplier supplier = new Supplier();
        supplier.setSupplierType(warehourseEnum.HAIWAIZIFANG.getCode());//海外资方
        List < Supplier > listSupplierZF = supplierService.findList(supplier);

        Supplier supplier1 = new Supplier();
        supplier1.setSupplierType(warehourseEnum.CABGKUFUWUSHANG.getCode());//仓库服务商
        List < Supplier > listSupplierFWS = supplierService.findList(supplier1);

        model.addAttribute("listCustomer", listCustomer);
        model.addAttribute("listSupplierFWS", listSupplierFWS);
        model.addAttribute("listSupplierZF", listSupplierZF);
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("twarehouse", twarehouse);
        return "modules/base/warehouse/twarehouseDetail";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("base:warehouse:twarehouse:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated Twarehouse twarehouse) {
        Double totalLoadingCharges1 = 0D;
        Double totalLoadingCharges2 = 0D;
        for (TwarehouseChargeItems twarehouseChargeItems : twarehouse.getTwarehouseChargeItemsList()) {
            totalLoadingCharges1 = NumberUtils.add(totalLoadingCharges1, twarehouseChargeItems.getUnitPrice() == null ? 0 : twarehouseChargeItems.getUnitPrice());
            totalLoadingCharges2 = NumberUtils.add(totalLoadingCharges2, twarehouseChargeItems.getAmount2()== null ? 0 : twarehouseChargeItems.getAmount2());
        }
        twarehouse.setTotalLoadingCharges1(totalLoadingCharges1);
        twarehouse.setTotalLoadingCharges2(totalLoadingCharges2);
        twarehouseService.save(twarehouse);
        return renderResult(Global.TRUE, text("保存仓库档案成功！"));
    }

    /**
     * 停用数据
     */
    @RequiresPermissions("base:warehouse:twarehouse:edit")
    @RequestMapping(value = "disable")
    @ResponseBody
    public String disable(Twarehouse twarehouse) {
        twarehouse.setStatus(Twarehouse.STATUS_DISABLE);
        twarehouseService.updateStatus(twarehouse);
        return renderResult(Global.TRUE, text("停用仓库档案成功"));
    }

    /**
     * 启用数据
     */
    @RequiresPermissions("base:warehouse:twarehouse:edit")
    @RequestMapping(value = "enable")
    @ResponseBody
    public String enable(Twarehouse twarehouse) {
        twarehouse.setStatus(Twarehouse.STATUS_NORMAL);
        twarehouseService.updateStatus(twarehouse);
        return renderResult(Global.TRUE, text("启用仓库档案成功"));
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("base:warehouse:twarehouse:delete")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(Twarehouse twarehouse) {
        twarehouseService.delete(twarehouse);
        return renderResult(Global.TRUE, text("删除仓库档案成功！"));
    }

    @RequiresPermissions("base:warehouse:twarehouse:view")
    @RequestMapping(value = {"getOneById", ""})
    public Twarehouse getOneById(Twarehouse twarehouse, Model model) {
        return twarehouseService.get(twarehouse.getWarehouseId());
    }

}